<div class="old customer">
    <h3>Khách hàng cũ</h3>
    <table class="table2">
        <tr>
            <td>Tìm kiếm</td>
            <td><?php echo $this->Form->input('Customer.query', array('type' => 'text', 'value' => isset($query) ? htmlspecialchars_decode($query) : '', 'class' => 'textfield', 'label' => false, 'div' => false)); ?> <span class="light">Tên, Mã khách, ĐT</span></td>
        </tr>
    </table>

    <script type="text/javascript">
    $(document).ready(function (){
        var timerid;
        <?php if (isset($query)): ?>
        $('#CustomerQuery').focus();
        <?php endif; ?>
        $('#CustomerQuery').keyup(function (e){
            if (e.keyCode == 13)
            {
                search($('#CustomerQuery').val());
            }
        });
        $('#CustomerQuery').click(function (e){
            $('#CustomerQuery').select();
        });
        function search(query)
        {
            //query = $('#CustomerQuery').val();
            clearTimeout(timerid);
            timerid = setTimeout(function (){
                $.ajax({
                    async: true,
                    type: "POST",
                    url: "add/query:" + query,
                    data: "data[Action][do]=find_customer",
                    success: function(msg){
                        $('#select_customer').html(msg);
                    },
                    beforeSend: function (XMLHttpRequest){
                        $("#busy-indicator").fadeIn();
                    },
                    complete: function (XMLHttpRequest, textStatus){
                        $("#busy-indicator").fadeOut();
                    }
                });
            }, 600);
        }
        var availableTags = [
            <?php
            if (!empty($customers))
            {
                $count = 0;
                $customers_count = sizeof($customers);
                foreach ($customers as $customer)
                {
                    echo "\"" . $customer['Customer']['information'] . "\"" . ($count < $customers_count ? "," : "");
                    $count++;
                }
            }
            ?>
        ];
        $( "#CustomerQuery" ).autocomplete({
            source: availableTags,
            select: function (event, ui){
                search(ui.item.value.trim());
            }
        });
    });
    </script>

    <?php if (!empty($selected_customer)): ?>
    <!-- <p><a id="clear-selected-customer" href="javascript:" class="underline"><strong>Xóa khách hàng này</strong></a></p> -->

    <table class="data">
        <tr>
            <td class="grey">Mã khách</td>
            <td><?php echo $selected_customer['Customer']['code'] ?></td>
        </tr>
        <tr>
            <td class="grey">Tên khách</td>
            <td><?php echo $selected_customer['Customer']['first_name'] ?>, <?php echo $selected_customer['Customer']['last_name'] ?></td>
        </tr>
        <tr>
            <td class="grey">ĐT</td>
            <td><?php echo $selected_customer['Customer']['telephone'] ?></td>
        </tr>
        <tr>
            <td class="grey">Email</td>
            <td><?php echo $selected_customer['Customer']['email'] ?></td>
        </tr>
        <tr>
            <td class="grey">Địa chỉ</td>
            <td><?php echo $selected_customer['Customer']['address'] ?></td>
        </tr>
        <?php echo $this->Form->input('Customer.selected_code', array('type' => 'hidden', 'value' => $selected_customer['Customer']['code'])); ?>
    </table>
    <?php endif; ?>

</div>

<div class="new customer">
    <h3>Khách hàng mới</h3>
    <?php if (!empty($success_msg)): ?>
    <div class="successful"><?php echo $success_msg; ?></div><br />
    <?php endif; ?>

    <?php echo $this->Form->create('Customer', array('url' => '/bookings/select_customer')); ?>
    <?php echo $this->Form->input('Action.do', array('type' => 'hidden', 'value' => 'add_customer')); ?>
    <table class="table2">
        <tr>
            <td>Họ và tên đệm <span class="compulsory">*</span></td>
            <td>
                <?php echo $this->Form->input('Customer.last_name', array('type' => 'text', 'class' => 'textfield', 'label' => false)); ?>
            </td>
        </tr>
        <tr>
            <td>Tên <span class="compulsory">*</span></td>
            <td>
                <?php echo $this->Form->input('Customer.first_name', array('type' => 'text', 'class' => 'textfield', 'label' => false)); ?>
            </td>
        </tr>
        <tr>
            <td>ĐT <span class="compulsory">*</span></td>
            <td><?php echo $this->Form->input('Customer.telephone', array('type' => 'text', 'class' => 'textfield', 'label' => false)); ?></td>
        </tr>
        <tr>
            <td>Email <span class="compulsory">*</span></td>
            <td><?php echo $this->Form->input('Customer.email', array('type' => 'text', 'class' => 'textfield', 'label' => false)); ?></td>
        </tr>
        <tr>
            <td>Địa chỉ</td>
            <td><?php echo $this->Form->input('Customer.address', array('type' => 'text', 'class' => 'textfield', 'label' => false)); ?></td>
        </tr>
        <tr>
            <td>Mã khách</td>
            <td>
                <?php echo $this->Form->input('Customer.code', array('type' => 'hidden', 'value' => $customer_code, 'class' => 'textfield', 'label' => false)); ?>
                <?php //echo $this->element('barcode', array('data_to_encode' => $customer_code)); ?>
                <span class="code"><?php echo $customer_code; ?></span>
            </td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td>
                <!-- <?php echo $this->Form->submit('Tạo mới', array('class' => 'button')); ?> -->
                <?php echo $this->Js->submit('Tạo mới', array('class' => 'button', 'update' => '#select_customer')); ?>
            </td>
        </tr>
    </table>
    <?php echo $this->Form->end(); ?>
</div>
<br class="clr" />
<?php echo $this->Js->writeBuffer(); ?>